package com.verisette.ncos.service.card.bizunit;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterCompany;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterTemplate;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.web.card.bean.CardCreateBean;

public class DefaultMittareCardByTemplateBU extends CommonBusinessUnit {
	protected Logger logger = Logger.getLogger(DefaultMittareCardByTemplateBU.class);
	
	protected float width=ProjectConstant.CARD_WIDTH;
	protected float height=ProjectConstant.CARD_HEIGHT;
	
	protected final String S_WATERMARK_TEXT = "Print@Post";
	
	protected final float S_WATERMARK_FONT_SIZE = 24;
	
	protected static final Integer[] TEMPLATE_IMG_REQUIRED = {4, 6, 8};
	protected static final Integer[] TEMPLATE_DEPT_REQUIRED = {5, 6, 7, 8};
	
	protected static final String[] PRINT_TYPE_FLAG = {"th", "en", "th/en"};
	
	protected static final Integer PRINT_TYPE_FLAG_DEF_IDX = 0;
	
	protected static final String BG_SALE_PATH = "\\BG\\BG-Sale.tif";

	protected static final String BG_OFFICER_PATH = "\\BG\\BG-Officer.tif";
	
//	protected static final String BG_PATH = "\\BG\\BG_Final_printpost.tif";
	protected static final String BG_PATH = BG_SALE_PATH;
	
	protected static final String MTP_REQUIRED_IMAGE_FLAG = "Y";
	
	protected static final String MITTARE_CC = "Mittare Contact Center 0-2640-7777";
	
	protected final String MOBILE_TITLE_TH = "มือถือ";
	protected final String TELEPHONE_TITLE_TH = "สายตรง";
	protected final String FAX_TITLE_TH = "โทรสาร";

	protected final String MOBILE_TITLE_EN = "Mobile";
	protected final String TELEPHONE_TITLE_EN = "Direct";
	protected final String FAX_TITLE_EN = "Fax";
	
	protected final String DELIMITER = ":";
	protected final String DELIMITER_PHONE = "/ ";
	
	protected final String AGENT_CODE_TITLE_TH = "รหัสตัวแทน ";
	protected final String AGENT_CODE_TITLE_EN = "Agent Code ";
	
	protected final String COMPANY_URL = "www.mittare.com";
	
	protected final BaseColor MITTARE_COLOR = new BaseColor(0, 122, 119);
	
	//Image box
//	protected final float IMG_BOX_WEIGHT=80.65f, IMG_BOX_HEIGHT=46.62f;
	protected final float IMG_BOX_WEIGHT=112.45f, IMG_BOX_HEIGHT=65f;
	protected final float IMG_BOX_LEFT=18.5f, IMG_BOX_BUTTOM=83f;
	protected final float IMG_BOX_BORDER_WEIGHT = 0f;

	protected void addWatermark(PdfContentByte over){
	}
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );
		
		MasterUserProfile userBean = (MasterUserProfile)processContext.getUserBean();
		CardCreateBean cardCreateBean = (CardCreateBean)processContext.getOriginalRequest().getValueObject();
		String userPDFPathDir = ProjectConstant.APP_TEMP_PDF_PREVIEW_FOLDER;
		String fullPDFPathFile = userPDFPathDir + "\\" + cardCreateBean.getOrder().getPdfPath();
		
		Rectangle pagesize = new Rectangle(width, height);
		Document document = new Document(pagesize, 0f, 0f, 0f, 0f);
		PdfWriter writer = null;
		
		File output=null;
		try{
			File dir = new File(userPDFPathDir);
			if(!dir.exists()) dir.mkdirs();
			
			output = new File(fullPDFPathFile);
			writer = PdfWriter.getInstance(document, new FileOutputStream(output));
			FontFactory.register(ProjectConstant.FONT_PSLxKittithada_NORMAL, ProjectConstant.FONT_NAME_PSLxKittithada_NORMAL);
			FontFactory.register(ProjectConstant.FONT_PSLxKittithada_BOLD, ProjectConstant.FONT_NAME_PSLxKittithada_BOLD);
			FontFactory.register(ProjectConstant.FONT_PSLxKittithada_ITALIC, ProjectConstant.FONT_NAME_PSLxKittithada_ITALIC);
			
	        document.open();
	        
	        Image personalPic;
	        Image backgroundPic;
	        float factor=0f;

    		String currentPageLanguage = PRINT_TYPE_FLAG[PRINT_TYPE_FLAG_DEF_IDX];
    		
    		String companyName = "";
    		
	        String name = "";
	        String position = "";
	        String agentCode = "";
	        String dept = "";

    		String officeName = "";
    		String officeAddr = "";
    		String officeAddr2 = "";
    		String officeDirect = "";
    		String officeFax = "";
    		String mobile = "";

	        String agentCodeTitle = AGENT_CODE_TITLE_TH;
    		String mobile_title = MOBILE_TITLE_TH;
    		String tel_title = TELEPHONE_TITLE_TH;
    		String fax_title = FAX_TITLE_TH;
    		
        	backgroundPic = Image.getInstance(String.format(ProjectConstant.APP_IMAGES_FOLDER + BG_PATH));
    		if (backgroundPic.getScaledWidth() > width || backgroundPic.getScaledHeight() > height) {
    			backgroundPic.scaleToFit(width, height);
    		}
    		document.add(backgroundPic);
    		
	        document.close();
		}catch(Exception ex){
			logger.error(ex.getStackTrace());
			ex.printStackTrace();
		}finally{
			if(writer!=null) try{ writer.close(); }catch(Exception ex){ logger.error("writer.close() fail."); }
		}
		
		if(logger.isDebugEnabled()){
			logger.debug("fullPDFPathFile::" + fullPDFPathFile);
			logger.debug("PDF (java.io.File)output::" + output);
		}
		
//		processContext.setValueObject(output);
	}
}